package com.pi.pro.xcmg.am.was.fac.service;



import com.fasterxml.jackson.annotation.JsonView;
import com.pi.common.CodesConstants;
import com.pi.common.utils.StringUtils;
import com.pi.common.utils.TextUtils;
import com.pi.core.exchange.vo.CommonNameRegistryService;
import com.pi.framework.core.bean.PageResult;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.XcmgAmSupport;
import com.pi.pro.xcmg.am.common.vo.CommonExchangeResultData;
import com.pi.pro.xcmg.am.common.vo.edi.XwmsInventoryLineDto;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.FuncReplyData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.FuncReplyListData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.StorageLineResultDto;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.TaskReportResultDto;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.*;
import com.pi.pro.xcmg.am.was.edi.infor.service.StorageServiceProxy;
import com.pi.pro.xcmg.am.was.edi.infor.service.TaskServiceProxy;
import com.pi.wms.base.BaseLotattrDto;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.dto.base.LocationLineDto;
import com.pi.wms.dto.inventory.InventoryChangeActionDto;
import com.pi.wms.entity.*;
import com.pi.wms.fac.LocationResolver;
import com.pi.wms.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 *
 */
@Service
public class InforCommonWorkService {


    @Autowired
    private CommonNameRegistryService xwmsNameRegistryService;

    @Autowired
    private DivisionService divisionService;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private LocationResolver locationResolver;

    @Autowired
    private SoService soService;

    @Autowired
    private SoDetailsService soDetailsService;

    @Autowired
    private InventoryLotattrService lotattrService;

    @Autowired
    private WbsTransferListService wbsTransferListService;

    @Autowired
    private WasCustomWorkService wasCustomWorkService;

    @Autowired
    private XwmsInventoryDetailsService xwmsInventoryDetailsService;

    //防重
    private static volatile boolean loading=false;


    /**
     * 从Infor导入待入库数据
     */
    public String loadStorageData(String divisioncode, StorageQueryData params){
        String m="";

        if (loading){
            return m;
        }
        loading = true;
        if (StringUtils.isNullOrBlank(divisioncode)){
            divisioncode= XcmgAmConstants.DIVISION_WH101_CODE;
        }
        Long divisionId = divisionService.retrieve(divisioncode);

        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(divisioncode);
        actionDto.setDivisionId(divisionId);
        List<StorageLineResultDto> lines=null;
        try{
            lines=this.queryInventory(params, actionDto);
            if (!StringUtils.isNullOrBlank(actionDto.getErrorText())){
                loading=false;
                m=actionDto.getErrorText();
                return m;
            }
            //
            xwmsInventoryDetailsService.cleanAll(divisionId);
            m = loadStorageData(divisionId, lines);
            loading = false;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            loading=false;
        }
        return m;
    }

    //
    public List<StorageLineResultDto> queryInventory(StorageQueryData params, TaskActionDto actionDto){
        List<StorageLineResultDto> lines=null;

        String m="";
        if (params==null){
            params=new StorageQueryData();
        }
        //
        Long divisionId=XcmgAmConstants.DIVISION_WH101_ID;
        if ((actionDto!=null) && (actionDto.getDivisionId()!=null)){
            divisionId=actionDto.getDivisionId();
        }
        //
        StorageServiceProxy proxy = null;
        FuncReplyListData<StorageLineResultDto> replyData=null;
        try{
            proxy = this.xwmsNameRegistryService.takeProxy(XcmgAmConstants.CLIENT_ID_XCMG, divisionId, StorageServiceProxy.class);
            if (proxy == null) {
                m = "not found task proxy";
                if (actionDto!=null){
                    actionDto.setErrorText(m);
                }
                return lines;
            }
            replyData=proxy.query(params);
            m=InforWasExchangeSupport.errorListMessage(replyData);
            if (!StringUtils.isNullOrBlank(m)){
                if (actionDto!=null){
                    actionDto.setErrorText(m);
                }
                return lines;
            }
            if ((replyData==null)||(replyData.getResultData()==null) || (replyData.getResultData().getProcessOutputs()==null) || (replyData.getResultData().getProcessOutputs().size()==0)){
                m="未能获取到X-WMS暂存收货区库存数据,请联系系统管理员.";
                if (actionDto!=null){
                    actionDto.setErrorText(m);
                }
                return lines;
            }
            lines = replyData.getResultData().getProcessOutputs();
        }catch (Exception e){
            e.printStackTrace();
        }
        return lines;
    }

    public List<StorageLineResultDto> queryAndGroupInventory(StorageQueryData params, TaskActionDto actionDto){
        List<StorageLineResultDto> linesall=null;
        if (actionDto==null){
            actionDto=new TaskActionDto();
            actionDto.setDivisionId(XcmgAmConstants.DIVISION_WH101_ID);
        }
        //
        linesall=this.queryInventory(params, actionDto);
        if (!StringUtils.isNullOrBlank(actionDto.getErrorText())){
            return linesall;
        }
        if ((linesall==null) || (linesall.size()==0)){
            linesall=new ArrayList<>();
            return linesall;
        }
        String supcode=null;
        if ((params!=null) && (params.getData()!=null)
                && (!StringUtils.isNullOrBlank(params.getData().getSuppliercode()))){
            supcode=params.getData().getSuppliercode().trim();
        }
        String key=null;
        StorageLineResultDto line=null;
        List<StorageLineResultDto> lines=new ArrayList<>();
        Map<String, StorageLineResultDto> map=new HashMap<>();
        try{
            for (StorageLineResultDto d:linesall){
                if (d==null){
                    continue;
                }
                //标签指定供应商
                if (supcode!=null){
                    if ((!StringUtils.isNullOrBlank(d.getLottable03()))
                            && (!d.getLottable03().equalsIgnoreCase(supcode))){
                        continue;
                    }
                }
                key=InforWasExchangeSupport.groupKey(d);
                line=map.get(key);
                if (line==null){
                    line=d;
                    map.put(key, line);
                    lines.add(line);
                }
                line.setStoragetotal(line.getStoragetotal().add(d.getQty()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return lines;
    }

    /**
     * 上报任务数据给Infor
     */
    /**
    public List<FuncReplyData<TaskReportResultDto>> reportTaskData(String divisionCode, List<TaskReportData> taskReports){
        String m="";
        FuncReplyData<TaskReportResultDto> resultData=InforWasExchangeSupport.emptyReply();
        List<FuncReplyData<TaskReportResultDto>> resultDatas=new ArrayList<>();
        if ((taskReports==null)||(taskReports.size()==0)){
            return resultDatas;
        }
        Long divisionId=this.divisionService.retrieveOn(divisionCode,XcmgAmConstants.DIVISION_WH101_ID);
        //
        TaskServiceProxy proxy = null;
        FuncRequestData<TaskReportData> reportData=null;
        try{
            proxy = this.xwmsNameRegistryService.takeProxy(XcmgAmConstants.CLIENT_ID_XCMG, divisionId, TaskServiceProxy.class);
            if (proxy == null) {
                m = "not found task proxy";
                resultData = InforWasExchangeSupport.errorReply(m);
                resultDatas.add(resultData);
                return resultDatas;
            }
            reportData=new FuncRequestData();
            for (TaskReportData tr:taskReports){
                if (tr==null){
                    continue;
                }
                reportData.setData(tr);
                resultData=proxy.report(reportData);
                if (reportData!=null){
                    resultDatas.add(resultData);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultDatas;
    }
    **/

    public FuncReplyData<TaskReportResultDto> reportTaskData(String divisionCode, TaskReportData taskReport){
        String m="";
        FuncReplyData<TaskReportResultDto> resultData=InforWasExchangeSupport.emptyReply();
        if (taskReport==null){
            return resultData;
        }
        Long divisionId=this.divisionService.retrieveOn(divisionCode, XcmgAmConstants.DIVISION_WH101_ID);
        //
        TaskServiceProxy proxy = null;
        FuncRequestData<TaskReportData> reportData=null;
        try{
            proxy = this.xwmsNameRegistryService.takeProxy(XcmgAmConstants.CLIENT_ID_XCMG, divisionId, TaskServiceProxy.class);
            if (proxy == null) {
                m = "not found task proxy";
                resultData = InforWasExchangeSupport.errorReply(m);
                return resultData;
            }
            reportData=new FuncRequestData();
            reportData.setData(taskReport);
            resultData=proxy.report(reportData);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultData;
    }

    //
    public List<FuncReplyData<TaskReportResultDto>> reportTask(String divisionCode, List<TwtActTaskList> tasks){
        String m="";

        FuncReplyData<TaskReportResultDto> resultData=InforWasExchangeSupport.emptyReply();
        List<FuncReplyData<TaskReportResultDto>> resultDatas=new ArrayList<>();
        if ((tasks==null)||(tasks.size()==0)){
            return resultDatas;
        }
        if (StringUtils.isNullOrBlank(divisionCode)){
            divisionCode=XcmgAmConstants.DIVISION_WH101_CODE;
        }
        TaskReportData taskReport=null;
        for (TwtActTaskList t:tasks){
            if (t==null){
                continue;
            }
            if (WmsConstants.TASK_STATUS_CLOSE.equalsIgnoreCase(t.getStatus())){
                continue;
            }
            if (WmsConstants.TASK_STATUS_CANCEL.equalsIgnoreCase(t.getStatus())){
                continue;
            }
            taskReport=this.translateTo(divisionCode, t);
            if (taskReport!=null){
                resultData=this.reportTaskData(divisionCode, taskReport);
                resultDatas.add(resultData);
            }
        }
        return resultDatas;
    }

    //导入任务(出库/拣货)
    public String loadTaskOrder(String divisionCode, List<TaskOrderDto> orderTasks){
        String m = "";

        m=InforWasExchangeSupport.checkOrder(orderTasks);
        if (!StringUtils.isNullOrBlank(m)){
            return m;
        }
        InforWasExchangeSupport.trimOrder(orderTasks);
        //
        Map<String, TwmBasMaterial> materials=new HashedMap();
        m=this.wasCustomWorkService.checkOrder(orderTasks, materials);
        if (!StringUtils.isNullOrBlank(m)){
            return m;
        }
        //
        TwaDocSoHeader header=null;
        TwaDocSoDetails line=null;
        List<TwaDocSoDetails> lines=null;
        String groupno=null;
        Map<Long, Integer> nextlinenum=new HashedMap();
        Map<String, TwaDocSoHeader> headermap=new HashedMap();
        Integer lineindex=null;
        try{
            for (TaskOrderDto t: orderTasks){
                if (t==null){
                    continue;
                }
                groupno=InforWasExchangeSupport.groupKey(t);
                header=headermap.get(groupno);
                if (header==null){
                    header = this.wasCustomWorkService.takeSoHeader(XcmgAmConstants.DIVISION_WH101_ID, t, XcmgAmConstants.WORK_SOURCE_PO, materials, InforWasXcmgExchangeConstants.XWMS_USER_NAME);
                }
                if (header==null){
                    m="创建出库单失败.";
                    return m;
                }
                if (header.isNew()){
                    this.soService.save(header);
                }
                if (!headermap.containsKey(groupno)){
                    headermap.put(groupno, header);
                }
                lineindex = nextlinenum.get(header.getId());
                if (lineindex==null){
                    lineindex=this.soDetailsService.maxLineNumber(header.getId());
                    if (lineindex<=0){
                        lineindex=0;
                    }
                    if (lineindex>10){
                        lineindex=lineindex /10;
                    }
                }
                lineindex++;
                line=this.wasCustomWorkService.createSoDetails(header, t, materials.get(t.getMCode()), lineindex);
                if (line!=null){
                    this.soDetailsService.save(line);
                    nextlinenum.put(header.getId(), lineindex);

                }
            }
        }catch (Exception e){
            m="导入出库单失败.异常:"+e.getMessage();
            e.printStackTrace();
        }
        return m;
    }

    public String transfer(InventoryTransferData transferData, String worker){
        String m = "";
        m = this.check(transferData);
        if (!StringUtils.isNullOrBlank(m)){
            return m;
        }
        this.prepare(transferData);
        TwmBasMaterial material=this.materialService.findByCode(transferData.getMCode());
        if (material==null){
            m="找不到对应的物料主数据.物料编码:"+transferData.getMCode();
            return m;
        }

        BaseLotattrDto fromLotattr=new BaseLotattrDto();
        fromLotattr.setClientId(material.getGlobalClientId());
        fromLotattr.setMaterialId(material.getId());
        fromLotattr.setLotattr05(this.wasCustomWorkService.defLotnumber());
        fromLotattr.setLotattr06(transferData.getSupplier());
        fromLotattr.setLotattr07(transferData.getOldWBSNo());

        BaseLotattrDto toLotattr=new BaseLotattrDto();
        toLotattr.setClientId(material.getGlobalClientId());
        toLotattr.setMaterialId(material.getId());
        toLotattr.setLotattr05(this.wasCustomWorkService.defLotnumber());
        toLotattr.setLotattr06(transferData.getSupplier());
        toLotattr.setLotattr07(transferData.getNewWBSNo());
        //
        InventoryChangeActionDto changeAction=new InventoryChangeActionDto();
        changeAction.setWorker(worker);
        changeAction.setBatchumber(TextUtils.genBatchNumber());
        changeAction.setOperationType(WmsConstants.INVENTORY_ACTION_TRANSFER);
        m = this.wasCustomWorkService.transfer(fromLotattr, toLotattr, transferData.getQuantity(), changeAction);
        TwtActWbsTransferList wbsTransfer=new TwtActWbsTransferList();
        wbsTransfer.setGlobalClientId(material.getGlobalClientId());
        wbsTransfer.setCustomerId(material.getCustomerId());
        wbsTransfer.setDivisionId(XcmgAmConstants.DIVISION_WH101_ID);
        wbsTransfer.setCustomerCode(transferData.getSupplier());
        wbsTransfer.setMaterialCode(transferData.getMCode());
        wbsTransfer.setMaterialName(material.getName());
        wbsTransfer.setMaterialSpec(material.getSpecModel());
        wbsTransfer.setSupplierCode(transferData.getSupplier());
        wbsTransfer.setSupplierName(wbsTransfer.getSupplierCode());
        wbsTransfer.setWbsNum(transferData.getOldWBSNo());
        wbsTransfer.setQuantity(transferData.getQuantity());
        wbsTransfer.setToMaterialCode(wbsTransfer.getMaterialCode());
        wbsTransfer.setToSupplierCode(wbsTransfer.getSupplierCode());
        wbsTransfer.setToWbsNum(transferData.getNewWBSNo());
        wbsTransfer.setQuantity(transferData.getQuantity());
        wbsTransfer.setOpenTime(new Date());
        wbsTransfer.setCloseTime(new Date());
        wbsTransfer.setCreateSource(InforWasXcmgExchangeConstants.SYS_XWMS);

        wbsTransfer.setUdf01(transferData.getBatchNo());
        wbsTransfer.setUdf02(null);
        wbsTransfer.setUdf03(null);
        wbsTransfer.setUdf04(null);
        wbsTransfer.setUdf05(null);
        wbsTransfer.setUdf06(null);
        wbsTransfer.setUdf07(null);
        wbsTransfer.setUdf08(null);
        wbsTransfer.setUdf09(null);
        wbsTransfer.setUdf10(null);
        wbsTransfer.setNotes(null);
        wbsTransfer.setCreatedBy(InforWasXcmgExchangeConstants.XWMS_USER_NAME);
        wbsTransfer.setCreatedDate(new Date());
        wbsTransfer.setLastModifiedBy(wbsTransfer.getCreatedBy());
        wbsTransfer.setLastModifiedDate(wbsTransfer.getCreatedDate());
        if (StringUtils.isNullOrBlank(m)){
            wbsTransfer.setCheckStatus(CodesConstants.COMMON_STATUS_CLOSED);
        }else {
            wbsTransfer.setCheckStatus(CodesConstants.COMMON_STATUS_APP_ERROR);
        }
        try{
            this.wbsTransferListService.save(wbsTransfer);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    private String check(InventoryTransferData transferData){
        String m = "";
        if (transferData==null){
            m="库存属性同步数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(transferData.getSupplier())){
            m="无效的库存属性同步,供应商为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(transferData.getMCode())){
            m="无效的库存属性同步,货品编码为空.";
            return m;
        }
        /**
        if (StringUtils.isNullOrBlank(transferData.getBatchNo())){
            m="无效的库存属性同步,WMS批次号为空.";
            return m;
        }
        **/
        /**
        if (StringUtils.isNullOrBlank(transferData.getOldWBSNo())){
            m="无效的库存属性同步,老WBS批为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(transferData.getNewWBSNo())){
            m="无效的库存属性同步,新WBS批为空.";
            return m;
        }
         **/
        if (transferData.getNewWBSNo().equalsIgnoreCase(transferData.getOldWBSNo())){
            m="无效的库存属性同步,新旧WBS批相同.";
            return m;
        }
        if ((transferData.getQuantity()==null) || (transferData.getQuantity().compareTo(BigDecimal.ZERO))<=0){
            m="无效的库存属性同步,新WBS批数量无效.";
            return m;
        }
        return m;
    }

    private String prepare(InventoryTransferData transferData){
        String m = "";
        if (transferData==null){
            return m;
        }
        transferData.setSupplier(transferData.getSupplier().trim());
        transferData.setMCode(transferData.getMCode().trim());
        if (StringUtils.isNullOrBlank(transferData.getOldWBSNo())){
            transferData.setOldWBSNo(CodesConstants.COMMON_WILDCARD);
        }
        transferData.setOldWBSNo(transferData.getOldWBSNo().trim());
        if (StringUtils.isNullOrBlank(transferData.getNewWBSNo())){
            transferData.setNewWBSNo(CodesConstants.COMMON_WILDCARD);
        }
        transferData.setNewWBSNo(transferData.getNewWBSNo().trim());
        return m;
    }

    //
    public String loadStorageData(Long divisionId, List<StorageLineResultDto> lines){
        String m = "";
        if ((lines==null) || (lines.size()==0)){
            return m;
        }
        String warns="";
        TwiInventoryXwmsDetails xline=null;
        TwmBasMaterial material=null;
        Map<String, TwmBasMaterial> newmaterialmap=new HashMap<>();
        try{
            for (StorageLineResultDto line:lines){
                m=InforWasExchangeSupport.check(line);
                if (!StringUtils.isNullOrBlank(m)){
                    warns=warns+";"+m;
                    continue;
                }
                line.setSku(line.getSku().trim());
                InforWasExchangeSupport.trim(line);
                material=this.materialService.lookup(StringUtils.ifNullOrBlank(line.getStorerkey(), XcmgAmConstants.CUSTOMER_CODE_XCMG_AM), line.getSku());
                if (material==null){
                    material=newmaterialmap.get(line.getSku());
                    if (material==null){
                        material=this.wasCustomWorkService.loadMaterial(line);
                        if (material==null){
                            //m="找不到物料主数据,请确认.物料码:"+line.getSku();
                            m="创建物料主数据出错,请联系系统管理员.物料码:"+line.getSku();
                            warns=warns+";"+m;
                            continue;
                        }
                        newmaterialmap.put(line.getSku(), material);
                    }
                }
                //
                xline=this.translateTo(divisionId, line, material);
                if (xline!=null){
                    this.xwmsInventoryDetailsService.save(xline);
                }
            }

            if (!StringUtils.isNullOrBlank(warns)){
                m=warns;
            }
        }catch (Exception e){
            m="保存X-WMS暂存库存异常.异常:"+e.getMessage();
            e.printStackTrace();
        }
        return m;
    }


    //-----------------------------------------------------------------------
    //
    //-----------------------------------------------------------------------
    public List<XwmsInventoryLineDto> translateTo(String divisionCode, List<StorageLineResultDto> lineDatas){
        List<XwmsInventoryLineDto> lines=new ArrayList<>();
        if ((lineDatas==null)||(lineDatas.size()==0)){
            return lines;
        }
        XwmsInventoryLineDto line=null;
        TwmBasMaterial material=null;
        for (StorageLineResultDto linedata:lineDatas){
            if (linedata==null){
                continue;
            }
            material=this.materialService.lookup(StringUtils.ifNullOrBlank(linedata.getStorerkey(), XcmgAmConstants.CUSTOMER_CODE_XCMG_AM), linedata.getSku());
            if (material==null){
                continue;
            }
            line=new XwmsInventoryLineDto();
            line.setDivisioncode(divisionCode);
            line.setCustomercode(linedata.getStorerkey());
            line.setMaterialcode(linedata.getSku());
            line.setMaterialname(linedata.getDescr());
            line.setMaterialspec(material.getSpecModel());
            line.setSuppliercode(linedata.getLottable03());
            line.setSuppliername("");
            line.setWbsnumber(linedata.getLottable01());

            line.setStorageqty(linedata.getQty());
            if ((linedata.getStoragetotal()!=null) && (linedata.getStoragetotal().compareTo(linedata.getQty())>0)){
                line.setStorageqty(linedata.getStoragetotal());
            }
            line.setStorageqtyeach(line.getStorageqty());
            line.setPalqty(linedata.getPaltotal());
            line.setPalqtyeach(line.getPalqty());
            line.setReceiveqty(linedata.getRecvtotal());
            line.setReceiveqtyeach(line.getReceiveqty());
            line.setUomCode(material.getReceiveUom());
            line.setUomName(material.getPrintUom());

            line.setLotattr01(null);
            line.setLotattr02(null);
            line.setLotattr03(null);
            line.setLotattr04(null);
            line.setLotattr05(null);
            line.setLotattr06(linedata.getLottable03()); //供应商编码
            line.setLotattr07(linedata.getLottable01());      //WBS批次
            line.setLotattr08(null);
            line.setLotattr09(null);
            line.setLotattr10(null);
            line.setLotattr11(null);
            line.setLotattr12(null);
            line.setLotattr13(null);
            line.setLotattr14(null);
            line.setLotattr15(null);
            line.setLotattr16(null);

            line.setLocationcode(linedata.getLoc());
            line.setContainernumber("");
            line.setTracenumber("");
            line.setReceivetime(linedata.getLottable04());
            line.setQcstatus("");

            line.setUdf01("");
            line.setUdf02("");
            line.setUdf03(linedata.getLottable01());  //WBS
            line.setUdf04(linedata.getLottable02());  //工厂库存地
            line.setUdf05(linedata.getLottable03());  //供应商代码
            line.setUdf06(linedata.getLottable04());  //收货日期,YYYY-MM-DD
            line.setUdf07(linedata.getLot());         //XWMS批
            line.setUdf08(linedata.getLoc());         //XWMS库位
            line.setUdf09("");
            line.setUdf10("");

            line.setGroupNumber(InforWasExchangeSupport.groupKey(linedata));

            lines.add(line);
        }
        return lines;
    }

    public CommonExchangeResultData translateTo(TaskActionDto actionDto){
        CommonExchangeResultData resultData= XcmgAmSupport.ok();
        if (actionDto==null){
            return resultData;
        }
        if (!StringUtils.isNullOrBlank(actionDto.getErrorText())){
            resultData= XcmgAmSupport.error(actionDto.getErrorCode(), actionDto.getErrorText(), "");
        }
        return resultData;
    }

    public TaskReportData translateTo(String divisionCode, TwtActTaskList task){
        TaskReportData taskReport=null;
        if (task==null){
            return taskReport;
        }
        TwmBasMaterial mat=this.materialService.lookup(task.getMaterialId());
        LocationLineDto fmloc=null;
        LocationLineDto toloc=null;

        taskReport=new TaskReportData();
        taskReport.setWhseid(this.wasCustomWorkService.xwmsWarehouseCodeOf(divisionCode));
        taskReport.setTaskkey(task.getBulkNumber());
        taskReport.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        taskReport.setSku(mat.getCode());
        taskReport.setLot01(StringUtils.ifNullOrBlank(task.getLotattr07(),""));

        taskReport.setQty(task.getToQty());
        taskReport.setLot(StringUtils.ifNullOrBlank(task.getUdf07(), ""));
        if (WmsConstants.TASK_TYPE_PUTAWAY.equalsIgnoreCase(task.getType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PUTAWAY);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
        }
        if (WmsConstants.TASK_TYPE_PICK.equalsIgnoreCase(task.getType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PICK);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_PICKTO);
        }
        if (WmsConstants.TASK_TYPE_MOVE.equalsIgnoreCase(task.getType())){
            fmloc=this.locationResolver.lookup(task.getFromLocId());
            toloc=this.locationResolver.lookup(task.getToLocId());
            if (WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(fmloc.getLocationType())){
                if (WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(toloc.getLocationType())){
                    taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                    taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                    taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                }else {
                    taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                    taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                    taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
                }
            }else {
                if (WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(fmloc.getLocationType())){
                    taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                    taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
                    taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                }else {
                    taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                    taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
                    taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
                }
            }
        }
        //
        if (CodesConstants.COMMON_WILDCARD.equalsIgnoreCase(taskReport.getLot01())){
            taskReport.setLot01("");
        }
        return taskReport;
    }

    public TaskReportData translateTo(String divisionCode, TwtActTransList trans){
        TaskReportData taskReport=null;
        if (trans==null){
            return taskReport;
        }
        TwmBasMaterial mat=this.materialService.lookup(trans.getMaterialId());
        LocationLineDto fmloc=null;
        LocationLineDto toloc=null;

        taskReport=new TaskReportData();
        taskReport.setWhseid(this.wasCustomWorkService.xwmsWarehouseCodeOf(divisionCode));
        taskReport.setTaskkey(trans.getCallNumber());
        taskReport.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        taskReport.setSku(mat.getCode());
        taskReport.setLot01(StringUtils.ifNullOrBlank(trans.getFromLotattr07(),""));

        taskReport.setQty(trans.getQtyTrans());
        taskReport.setLot(StringUtils.ifNullOrBlank(trans.getUdf17(), ""));

        if (WmsConstants.TRANS_TYPE_RECEIVE.equalsIgnoreCase(trans.getType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PUTAWAY);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
        }
        if (WmsConstants.TRANS_TYPE_SHIP.equalsIgnoreCase(trans.getType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PICK);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_PICKTO);

            if (InforWasXcmgExchangeConstants.TASK_TYPE_MOVE.equalsIgnoreCase(trans.getUdf16())){
                taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
            }
        }
        taskReport.setTransid(trans.getId());
        //
        if (CodesConstants.COMMON_WILDCARD.equalsIgnoreCase(taskReport.getLot01())){
            taskReport.setLot01("");
        }
        return taskReport;
    }


    public TaskReportData translateTo(String divisionCode, TwiActTransList trans){
        TaskReportData taskReport=null;
        if (trans==null){
            return taskReport;
        }
        TwmBasMaterial mat=this.materialService.lookup(trans.getMaterialId());
        LocationLineDto fmloc=null;
        LocationLineDto toloc=null;

        taskReport=new TaskReportData();
        taskReport.setWhseid(this.wasCustomWorkService.xwmsWarehouseCodeOf(divisionCode));
        taskReport.setTaskkey(trans.getRefTaskNumber());
        taskReport.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        taskReport.setSku(mat.getCode());
        taskReport.setLot01(StringUtils.ifNullOrBlank(trans.getFromLotNumber(),""));

        taskReport.setQty(trans.getQtyTrans());
        taskReport.setLot(StringUtils.ifNullOrBlank(trans.getUdf17(), ""));

        if (WmsConstants.TRANS_TYPE_RECEIVE.equalsIgnoreCase(trans.getTransType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PUTAWAY);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
        }
        if (WmsConstants.TRANS_TYPE_SHIP.equalsIgnoreCase(trans.getTransType())){
            taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_PICK);
            taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
            taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_PICKTO);

            if (InforWasXcmgExchangeConstants.TASK_TYPE_MOVE.equalsIgnoreCase(trans.getUdf16())){
                taskReport.setType(InforWasXcmgExchangeConstants.TASK_TYPE_MOVE);
                taskReport.setFromloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK010000);
                taskReport.setToloc(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_LOC_LK01S);
            }
        }
        taskReport.setTransid(trans.getId());
        //
        if (CodesConstants.COMMON_WILDCARD.equalsIgnoreCase(taskReport.getLot01())){
            taskReport.setLot01("");
        }
        return taskReport;
    }

    private TwiInventoryXwmsDetails translateTo(Long divisionId, StorageLineResultDto line, TwmBasMaterial material){
        //
        TwiInventoryXwmsDetails storage = null;
        if (divisionId==null){
            divisionId = XcmgAmConstants.DIVISION_WH101_ID;
        }
        if (line==null){
            return storage;
        }
        if (material==null){
            return storage;
        }
        try{
            storage=new TwiInventoryXwmsDetails();
            storage.setGlobalClientId(material.getGlobalClientId());
            storage.setCustomerId(material.getCustomerId());
            storage.setDivisionId(divisionId);
            storage.setCustomerCode(line.getStorerkey());
            storage.setMaterialId(material.getId());
            storage.setMaterialCode(line.getSku());
            storage.setMaterialName(line.getDescr());
            storage.setMaterialSpec(material.getSpecModel());
            storage.setSupplierCode(line.getLottable03());
            storage.setSupplierName(null);
            storage.setWbsNum(line.getLottable01());   //WBS批次
            storage.setStorageQty(line.getQty());
            storage.setStorageQtyEach(storage.getStorageQty());
            storage.setPalQty(BigDecimal.ZERO);
            storage.setPalQtyEach(storage.getPalQty());
            storage.setReceiveQty(BigDecimal.ZERO);
            storage.setReceiveQtyEach(storage.getReceiveQty());
            storage.setUomCode(CodesConstants.UOM_EACH);
            storage.setUomName(material.getPrintUom());
            storage.setLotattr01(null);
            storage.setLotattr02(null);
            storage.setLotattr03(null);
            storage.setLotattr04(null);
            storage.setLotattr05(null);
            storage.setLotattr06(null); //供应商编码
            storage.setLotattr07(null);//WBS
            storage.setLotattr08(null);
            storage.setLotattr09(null);
            storage.setLotattr10(null);
            storage.setLotattr11(null);
            storage.setLotattr12(null);
            storage.setLotattr13(null);
            storage.setLotattr14(null);
            storage.setLotattr15(null);
            storage.setLotattr16(null);
            if (!StringUtils.isNullOrBlank(line.getLottable03())){
                storage.setLotattr06(line.getLottable03().trim()); //供应商编码
            }
            if (!StringUtils.isNullOrBlank(line.getLottable01())){
                storage.setLotattr07(line.getLottable01().trim()); //WBS
            }
            storage.setStorageLocCode(line.getLottable02());
            storage.setXwmsLotNumber(line.getLot());
            storage.setXwmsLocCode(line.getLoc());
            storage.setXwmsReceiveTime(line.getLottable04());
            storage.setCreateSource(InforWasXcmgExchangeConstants.SYS_XWMS);
            storage.setCheckStatus(CodesConstants.COMMON_STATUS_CREATED);
            storage.setLoadStatus(CodesConstants.COMMON_STATUS_CREATED);
            storage.setUdf01(null);
            storage.setUdf02(null);
            storage.setUdf03(line.getLottable01());  //WBS
            storage.setUdf04(line.getLottable02());  //工厂库存地
            storage.setUdf05(line.getLottable03());  //供应商代码
            storage.setUdf06(line.getLottable04());  //收货日期,YYYY-MM-DD
            storage.setUdf07(line.getLot());         //XWMS批
            storage.setUdf08(line.getLoc());         //XWMS库位
            storage.setUdf09(null);
            storage.setUdf10(null);
            storage.setEnabled(true);
            storage.setEditable(true);
            storage.setNotes(null);

            storage.setCreatedBy(InforWasXcmgExchangeConstants.USER_XWMS_WAS_INVENTORY_LOADER);
            storage.setCreatedDate(new Date());
            storage.setLastModifiedBy(storage.getCreatedBy());
            storage.setLastModifiedDate(storage.getCreatedDate());
        }catch (Exception e){
            e.printStackTrace();
        }
        return storage;
    }


    public Long takeDivisionId(StorageLineResultDto storageLine){
        return XcmgAmConstants.DIVISION_WH101_ID;
    }


}
